package tmt.usercenter.web.domain.enums;

import com.tmt.annotation.EnumDescriptor;
import com.tmt.annotation.parser.EnumAnnotationParser;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

/**
 * 审核状态
 * Created by ThreeManTeam on 2017/4/27.
 */
public enum AuditStatus {

    @EnumDescriptor( title = "未申请审核", description = "未申请审核", groupId = "petitioner")
    NOT_APPLY,

    @EnumDescriptor( title = "申请审核中", description = "申请审核中", groupId = "petitioner")
    AUDITING,

    @EnumDescriptor( title = "审核成功", description = "审核成功", groupId = "auditor")
    AUDIT_SUCCESS,

    @EnumDescriptor( title = "审核失败", description = "审核失败", groupId = "auditor")
    AUDIT_FAIL;

    public static AuditStatus from(String title) {
        return EnumAnnotationParser.convertFromTitle(AuditStatus.class, title);
    }

    /**
     * 状态迁移控制
     * @param oldStatus
     * @param newStatus
     * @return
     */
    public static boolean canSwitch(AuditStatus oldStatus, AuditStatus newStatus){
        HashMap<AuditStatus, List<AuditStatus>> map = new HashMap<AuditStatus, List<AuditStatus>>(){{
            put(NOT_APPLY, Collections.singletonList(AUDITING));
            put(AUDITING, Arrays.asList(NOT_APPLY, AUDIT_SUCCESS, AUDIT_FAIL));
            put(AUDIT_SUCCESS, Collections.singletonList(AUDITING));
            put(AUDIT_FAIL, Collections.singletonList(AUDITING));
        }};
        return map.get(oldStatus).contains(newStatus);
    }

}
